/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

/**
 *
 * @author Muhammad Anis
 */
public class commandProcessor {

    public String input;
    public String command;
    public String arg1="", arg2="";
    public String[] listCommand = {"create","join","chat","addfriend","exit","info"
    ,"kick","ban","unban","persist","setowner"};

    //constructor
    public commandProcessor(String input) throws Exception {
        this.input = input;
        processCommand(input);
    }

    private void processCommand(String input) throws Exception {        
        if (input.trim().equals("")) {
            throw new Exception("Command cannot empty");
        } else {
            input.replace('/', ' ');
            input = input.trim();
            String[] hasil = input.split("\\s+");
            if (hasil.length < 1) {
                throw new Exception("Your Comment is Not Long Enough. Usage /<command> <parameter>");
            } else {
                this.command = hasil[0];
                if(!in_array(this.command, hasil))
                    throw new Exception("Your Command is not in Our command List");
                //cek command dengan panjang minimal dua
                if(!command.equals("exit")){
                    if(hasil.length < 2){
                        throw new Exception("Parameter Required. Usage: /<command><parameter> ");
                    }
                    this.arg1 = hasil[1];
                    if(hasil.length > 2){
                        this.arg2 = hasil[2];
                    }
                }
            }
        }
    }
    public boolean in_array(String needle,String[] stray){
        for(int i=0;i<stray.length;i++){
            if(stray[i].equals(needle))
                return true;
        }
        return false;
    }
}
